<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    Core
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: SearchController.php 9076 2011-07-21 02:11:10Z john $
 * @author     John
 */

/**
 * @category   Application_Core
 * @package    Core
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Ynadvsearch_SearchController extends Core_Controller_Action_Standard
{
  public function index2Action()
  {
  	$searchApi = Engine_Api::_()->getApi('search', 'ynadvsearch');
	$params = $this->_getAllParams();
	// check public settings
    $require_check = Engine_Api::_()->getApi('settings', 'core')->core_general_search;
    if( !$require_check ) {
      if( !$this->_helper->requireUser()->isValid() ) return;
    }
  	$searchModulesTable = new Ynadvsearch_Model_DbTable_Modules;
    $modulesObject = $searchModulesTable->getEnabledModules();
  	$modules = $modulesObject->toArray();
  	$types = array();
  	$page = (int) $this->_getParam('page',1);
  	if(isset($params['submit'])) {
  		$mods = $params['moduleynsearch'];
  		foreach ($mods as $value => $key) {
			foreach($modules as $module) {
				if ($module['name'] == $value) {
					$module['checked'] = $key;
					$results[] = $module;
					break;
				}
			}
			if ($key == 1) {
				$types[] = $searchModulesTable->getTypes($value);
			}
		}
		$page = 1;
		$this->view->modules = $results;
	}
  	else {
  		if (@$this->_getParam('module_result')) {
  			$this->view->modules = $this->_getParam('module_result');
  		}
  		else {
  			$this->view->modules = $modules;
  		}
  	}
  	$this->view->query = $query = (string) @$params['query'];
	$qty = $this->_getParam('qty');
	if (!is_numeric($qty) || $qty <= 0) {
		$qty = 10;
	}
  	$this->view->qty = $qty; 
	if (!$types) {
	 	$types = @$params['type'];
	}
	if (!$types && (@$params['is_search'])) {
		$this->view->checkAll = 1;
		$types = $searchModulesTable->getAllEnabledTypes($modulesObject);
	}
	$this->view->types = $types;
	if( $query ) {
	    $this->view->paginator = $paginator = $searchApi->getPaginator($query, $types);
	    $this->view->paginator->setCurrentPageNumber($page);
	    $this->view->page = $page;
	    $paginator->setItemCountPerPage($qty);

	}
  	if (@$params['checkall']) {
		$this->view->checkAll = 1;
	}
  }
  public function getLabelType($type){
	return strtoupper('ITEM_TYPE_' . $type);
  }
  
  public function indexAction() {
  	$values = $this->_getAllParams();
  	Zend_Registry::set('Search_Params', $values['query']);
  	$this->_helper->content
        ->setNoRender()
        ->setEnabled();
  }
}